package com.appkubes.fintech.admin.application.order.service.impl;


import com.alibaba.fastjson.JSON;
import com.appkubes.fintech.admin.application.bill.dao.RentBillHeaderDao;
import com.appkubes.fintech.admin.application.bill.enums.RentOrderEnums;
import com.appkubes.fintech.admin.application.credit.service.CreditService;
import com.appkubes.fintech.admin.application.mall.MallService;
import com.appkubes.fintech.admin.application.order.dao.RentOrderDao;
import com.appkubes.fintech.admin.application.order.entity.dto.RentOrderDTO;
import com.appkubes.fintech.admin.application.order.entity.vo.RentOrderVO;
import com.appkubes.fintech.admin.application.order.service.RentOrderService;
import com.appkubes.fintech.common.response.Response;
import com.appkubes.fintech.common.util.ConversionUtil;
import com.appkubes.fintech.common.util.StringUtils;
import com.appkubes.fintech.core.po.FintechRentBillHeader;
import com.appkubes.fintech.core.po.FintechRentBillHeaderExample;
import com.appkubes.fintech.core.po.FintechRentOrder;
import com.appkubes.fintech.core.po.FintechRentOrderExample;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class RentOrderServiceImpl implements RentOrderService {

    @Resource
    private RentOrderDao orderRepository;
    @Resource
    MallService mallService;
    @Resource
    private CreditService creditService;
    @Resource
    private RentBillHeaderDao billHeaderDao;

    @Override
    public PageInfo<RentOrderDTO> pageOrder(RentOrderVO request) {
        PageHelper.startPage(request.getPage(), request.getPageSize());
        List<RentOrderDTO> fintechRentOrderList = orderRepository.queryPage(request);
        fintechRentOrderList.forEach(e->{
            FintechRentBillHeaderExample example=new FintechRentBillHeaderExample();
            example.createCriteria().andOrderKeyEqualTo(e.getOrderKey());
            List<FintechRentBillHeader> headerList=billHeaderDao.selectByExample(example);
            e.setBillAmt(0);
            if(headerList.size()>0){
                FintechRentBillHeader header=headerList.get(0);
                e.setBillAmt((header.getBillAmount()+header.getBillInterest()));
            }
            if(StringUtils.isNotNull(e.getAttrJson())){
                Map<String,String> paraMap= JSON.parseObject(e.getAttrJson(),Map.class);
               e.setMemory(paraMap.get("内存"));
               e.setColour(paraMap.get("颜色"));
            }
        });
        return new PageInfo<>(fintechRentOrderList);
    }

    /**
     * @description: genj
     * @author: xqc
     * @date: 2020/1/1 11:25
     * @params:
     * @return:
     */
    @Override
    public FintechRentOrder getById(String id) {
        return orderRepository.selectByPrimaryKey(id);
    }

    /**
     * @Author: XQC 2020/1/12/10:58
     * @params:
     * @return:
     * @Description:
     **/
    @Override
    public List<RentOrderDTO> getByUserKey(String userKey) {
        FintechRentOrderExample example = new FintechRentOrderExample();
        FintechRentOrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserKeyEqualTo(userKey);
        criteria.andStatusEqualTo(RentOrderEnums.OVER.name());
        List<FintechRentOrder> orderList = orderRepository.selectByExample(example);
        List<RentOrderDTO> dtoList = ConversionUtil.ofList(orderList, RentOrderDTO.class);
        return dtoList;
    }

    @Override
    public Response sureOrder(@RequestBody RentOrderVO request) {
        String[] orderIds = request.getOrderKey().split(",");
        for (String orderId : orderIds) {
            FintechRentOrder order = orderRepository.selectByPrimaryKey(orderId);
            if (order.getPayStatus() == null || order.getPayStatus() == 0 || order.getPayStatus() == 2) {
                return Response.fail("订单未支付，不能确认");
            }
            if (order.getIsSure() != null && order.getIsSure() != 0) {
                return Response.fail("订单已确认");
            }
            if (order.getIsSure() == null || 0 == order.getIsSure()) {
                order.setIsSure(Integer.valueOf(request.getStatus()));
                order.setUpdateTime(new Date());
                orderRepository.updateByPrimaryKey(order);
                if(Objects.equals("2",order.getStatus())){  //订单拒绝额度要回退
                    creditService.unfrozenCredit(order.getUserKey(),order.getRentAmount()); //额度恢复
                }
                //通知商城系统
                mallService.notifyOrderEnsure(order.getUserKey(), order.getOrderKey(), order.getIsSure());
            }
        }
        return Response.success();
    }

    @Override
    public Response deleteOrder(RentOrderVO request) {
        creditService.deleteOrder(request.getUserKey(),request.getRentOrderNo());
        return Response.success();
    }
}
